package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 科举考试任务
 *
 * @author: 舟小亢
 * @date: 2023-08-30 19:17
 */
public class ExamTaskImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(ExamTaskImpl.class);

    public ExamTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "科举考试";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【科举考试任务】开始！");

        // 判断时间是否为17:00~24:00，日期是否为周一到周五
        if (!DateUtil.between(1, 5) || !TimeUtil.between(17, 23)) {
            LOG.info("【科举考试任务】未在活动时间！");
            return false;
        }

        // 找到活动并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_活动)) {
            return false;
        }

        // 找到日常活动并点击
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_日常活动)) {
            return false;
        }

        // 找到三界奇缘并参加
        MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, 1000);

        for (int i = 0; i < ParamConstants.UNIFY_TIMES; i++) {
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.科举考试_完成图标, 2, 0.95);
            if (pic.isFinded()) {
                // 摁ESC，退出
                ThreadUtil.sleep1();
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                LOG.info("【科举考试任务】已完成！");
                return false;
            }
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.科举考试_任务图标, 2);
            if (pic.isFinded()) {
                break;
            }
            // 如果没找到下拉滑动屏幕寻找图标
            MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

            // 一直没找到，可能任务时间还没开始
            if (i == ParamConstants.UNIFY_TIMES - 1) {
                LOG.error("【科举考试任务】任务失败！");
                return false;
            }
        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.ATTEND_TASK_DXY.getX(),
                pic.getPicY() + Coord.ATTEND_TASK_DXY.getY());

        // 循环答题
        for (int i = 0; i < 30; i++) {
            // 先找x/10，找到了就表示答题完毕
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.科举考试_结束标志, 1).isFinded()) {
                // 摁ESC，退出
                ThreadUtil.sleep1();
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                break;
            }
            // 点击答题，如果C没有，则答A
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.科举考试_答题C, 1)) {
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.科举考试_答题A, 2);
            }
        }

        LOG.info("【科举考试任务】完成！");
        return true;
    }

    @Override
    public boolean checkTask() {
        return true;
    }
}
